﻿using System;
using System.Collections.Generic;
using System.Text;
using LHS.Core.TableInfo;
using System.Collections.Specialized;
using System.Data;

namespace LHS.Core.Clinical
{
    public class ClinicalController : MS.Core.Base.Controller.BaseController
    {
        private CommonClinical _objClinical;

        private CommonClinical objClinical
        {
            get
            {
                if (_objClinical == null)
                {
                    _objClinical = new CommonClinical();
                }
                return _objClinical;
            }
        }

        /// <summary>
        /// Get new 1 clrISN trong bảng ClinicalRecord
        /// </summary>
        /// <returns></returns>
        public string GetNewCLRISN()
        {
            try
            {
                return objClinical.GetNewClrISN();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lấy ra mã bệnh nhân mới nhất trong bảng Patients
        /// </summary>
        /// <returns></returns>
        public string GetPATID()
        {
            try
            {
               return objClinical.GetPATID();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// get SCKISN trong bang Sicks theo SckName
        /// </summary>
        /// <param name="strSckName"></param>
        /// <returns></returns>
        public int GetSckISN(string strSckName)
        {

            try
            {
                ListDictionary param = new ListDictionary();
                param.Add(InfoSicks.SCKNAME, strSckName);
                object isckISN = base.GetScalar(InfoSicks.SCKISN, param, InfoSicks.TABLENAME);
                return isckISN is int ? (int)isckISN : 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// insert ClinicalRecord theo store sp_Patients_ClinicalRecordNurse_Insert
        /// </summary>
        /// <param name="param"></param>
        public void InsertClinicalRecord(ListDictionary param)
        {
            try
            {
                objClinical.InsertClinicalRecord(param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lấy ra cây triệu chứng
        /// </summary>
        public DataTable GetSickTree()
        {
            try
            {
                return objClinical.GetSickTree();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Lấy ra thông tin bệnh nhân có cấp thuốc
        /// </summary>
        /// <param name="strPatID"></param>
        /// <param name="strPatName"></param>
        /// <returns></returns>
        public DataTable GetPatientHasPrescriptionDT(string strPatID, string strPatName)
        {
            try
            {
                return objClinical.GetPatientHasPrescriptionDT(strPatID, strPatName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        ///  su dung cho form BAO CAO CUOI NGAY
        /// </summary>
        /// <param name="blnCheckAll">neu truyền check all bằng true, thì sẽ lọc theo nhân viên</param>
        /// <param name="strDateFrm"></param>
        /// <param name="strDateTo"></param>
        /// <returns></returns>
        public DataTable GetClinicalInvoices(bool blnCheckAllEmpISN, string strDateFrm, string strDateTo, string strEmpISN)
        {
            try
            {
                return objClinical.GetClinicalInvoices(blnCheckAllEmpISN, strDateFrm, strDateTo, strEmpISN);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// su dung cho form BAO CAO CUOI NGAY
        /// </summary>
        /// <param name="blnCheckAllSick"></param>
        /// <param name="blnCheckAllEmpISN"></param>
        /// <param name="strDateFrm"></param>
        /// <param name="strDateTo"></param>
        /// <param name="strEmpISN"></param>
        /// <param name="strPltISN"></param>
        /// <returns></returns>
        public DataTable GetPathologicalClinical(bool blnCheckAllSick, bool blnCheckAllEmpISN, string strDateFrm, string strDateTo, string strEmpISN, string strPltISN)
        {
            try
            {
                return objClinical.GetPathologicalClinical(blnCheckAllSick, blnCheckAllEmpISN, strDateFrm, strDateTo, strEmpISN, strPltISN);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        ///  su dung cho form BAO CAO CUOI NGAY
        /// </summary>
        /// <param name="blnCheckAllEmpISN"></param>
        /// <param name="strDateFrm"></param>
        /// <param name="strDateTo"></param>
        /// <param name="strEmpISN"></param>
        /// <returns></returns>
        public DataTable GetPathologicalSignsOutList(bool blnCheckAllEmpISN, string strDateFrm, string strDateTo, string strEmpISN)
        {
            try
            {
                return objClinical.GetPathologicalSignsOutList(blnCheckAllEmpISN, strDateFrm, strDateTo, strEmpISN);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Su dung trong form DiagnoseSyptom
        /// </summary>
        /// <returns></returns>
        public DataTable GetInnerScan()
        {
            try
            {
                return objClinical.GetInnerScan();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// su dung trong form HO SO BENH AN
        /// </summary>
        /// <param name="strID"></param>
        /// <param name="num"></param>
        /// <param name="num2"></param>
        /// <returns></returns>
        public DataTable Getfn_MaxRexamTimesOFMaxExamTimes(string strID, string num, string num2)
        {
            try
            {
                return objClinical.Getfn_MaxRexamTimesOFMaxExamTimes(strID, num, num2);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lay ra lan tai kham moi nhat cua lan kham truyen vao, khong join voi bang medicineExport
        /// </summary>
        /// <param name="strID"></param>
        /// <param name="num"></param>
        /// <param name="num2"></param>
        /// <returns></returns>
        public DataTable Getfn_MaxRexamTimesOFMaxExamTimesNoJoinMedExport(string strID, string num, string num2)
        {
            try
            {
                return objClinical.Getfn_MaxRexamTimesOFMaxExamTimesNoJoinMedExport(strID, num, num2);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lay thong tin phau thuat tu chuoi phau thuat san co truoc do
        /// </summary>
        /// <param name="strID"></param>
        /// <param name="num"></param>
        /// <param name="num2"></param>
        /// <returns></returns>
        public DataTable getSurgical(string strPhauThuat)
        {
            try
            {
                return objClinical.getSurgical(strPhauThuat);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Lay ra suffix tu ngay kham truoc do
        /// </summary>
        /// <param name="strPatID"></param>
        /// <param name="intLanKham"></param>
        /// <param name="intLanTaiKham"></param>
        /// <returns></returns>
        public string getSuffixFromExaminationDate(string strPatID, int intLanKham, int intLanTaiKham,DateTime dteNgayKhamHienTai)
        {
            try
            {
                /*•	Số ngày
                    Nếu lần đầu tiên kí hiệu: B
                    Nếu lần sau: 
                    A + số ngày + d (hoặc M, hoặc Y)
                    < 3 tháng số ngày + d
                    < 3 năm số tháng + m
                    >3 năm số năm + d
                    Làm tròn*/

                string strSuffix = intLanTaiKham == 0 ? "B" : "A";
                if (intLanTaiKham != 0)
                {
                    ///tim ra lan kham dau tien co phau thuat
                    int lantaikham = objClinical.getLanTaiKhamHasPT(strPatID, intLanKham);
                    ///sau do thuc hien theo cong thuc tren de tim ra ngay hau phau
                    DataTable dtNgayKhamTruoc = objClinical.getExaminationDate(strPatID, intLanKham, lantaikham);
                    if (dtNgayKhamTruoc != null && dtNgayKhamTruoc.Rows.Count > 0)
                    {
                        object objNgayKhamBefore = dtNgayKhamTruoc.Rows[0][InfoClinicalRecord.CLREXAMINATIONDATE];
                        if (objNgayKhamBefore is DateTime)
                        {
                            DateTime dteNgayKhamBefore = DateTime.Parse(objNgayKhamBefore.ToString());
                            if ((dteNgayKhamHienTai.Year - dteNgayKhamBefore.Year) > 3)
                            {
                                strSuffix += (dteNgayKhamHienTai.Year - dteNgayKhamBefore.Year).ToString() + "y";
                            }
                            else if (((TimeSpan)(dteNgayKhamHienTai - dteNgayKhamBefore)).Days > 3*30)
                            {
                                strSuffix +=  Math.Round((decimal)((TimeSpan)(dteNgayKhamHienTai - dteNgayKhamBefore)).Days/30) + "m";
                            }
                            else if (((TimeSpan)(dteNgayKhamHienTai - dteNgayKhamBefore)).Days== 0)
                            {
                                strSuffix = "B";
                            }
                            else
                            {
                                int iday = ((TimeSpan)(dteNgayKhamHienTai - dteNgayKhamBefore)).Days + 1;
                                strSuffix += iday.ToString() + "d";
                            }
                        }
                        else
                        {
                            strSuffix = "B";
                        }
                    }
                    else
                    {
                        strSuffix = "B";
                    }
                }
                return strSuffix;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

         /// <summary>
        /// lay ra toan bo hinh anh cua benh nhan
        /// </summary>
        /// <param name="strPatID"></param>
        /// <returns></returns>
        public DataTable getPicture(string strPatID)
        {
            try
            {
                return objClinical.getPicture(strPatID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lay thong tin phau thuat
        /// </summary>
        /// <param name="strPatID"></param>
        /// <param name="intLanKham"></param>
        /// <returns></returns>
        public DataTable getSurgicalInfo(string strPatID, int intLanKham)
        {
            try
            {
                ///tim ra lan kham dau tien co phau thuat
                int lantaikham = objClinical.getLanTaiKhamHasPT(strPatID, intLanKham);
                return objClinical.getSurgicalInfo(strPatID, intLanKham, lantaikham);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// lay thong tin phau thuat
        /// </summary>
        /// <param name="strPatID"></param>
        /// <param name="intLanKham"></param>
        /// <returns></returns>
        public int GetNewCLRPIC(string clrISN)
        {
            try
            {
                return objClinical.GetNewCLRPIC(clrISN);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
